/* $RCSFile: HtmlTabEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/HTMLTab/HtmlTabEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:55  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Security;
using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Settings.PageModules.HtmlTab;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.HtmlTab
{
    public partial class HtmlTabEdit : AddEditItemPage
    {
		
		protected AddonNice.UI.WebControls.IHtmlEditor DesktopText;

		HtmlTabDB HtmlTabDB      =   new HtmlTabDB();
		

        public HtmlTabEdit() : base(true,true,true,true)
        {
        }

		/// <summary>
        /// The Page_Load event on this Page is used to obtain the ModuleID
        /// and ItemID of the HtmlTab to edit.
        /// It then uses the AddonNice.HtmlTabsDB() data component
        /// to populate the page's edit controls with the annoucement details.
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e) 
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HtmlTabsEdit Page_Load isPostBack: {0}.",IsPostBack),PageTraceSwitch.Sw.Info);
            // If the page is being requested the first time, determine if an
            // HtmlTab itemID value is specified, and if so populate page
            // contents with the HtmlTab details
			DesktopText					                    =   GetEditor(PlaceHolderHTMLEditor);

			if (!IsPostBack) 
            {
                if ( !moduleSettingsDic[LStr.TabsMultiRow].ToBool() )
                    cbIsBreak.Enabled                       =   false;
                if ( !isNewItem ) 
                {
                    // Obtain a single row of HtmlTab information
					SqlDataReader dr                        =   null;
					try
					{
						dr                                  =   HtmlTabDB.GetSingleTabGlobalized(ItemID);

						// Load first row into DataReader
						if (dr.Read())
						{
							TitleField.Text                 =   (string)dr["Title"];
                            TbOrder.Value                   =   (int)dr["TabOrder"];
							cbIsBreak.Checked               =   (bool)dr["IsBreak"];
							DesktopText.TextContent         =   (string)dr["Description"];
							CreatedDate.Text                =   ((DateTime)dr["CreatedDate"]).ToShortDateString();
						}
					}
					catch (Exception ex)
					{
						if (PageTraceSwitch.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabsEdit Page_Load  ex : {0}", ex),PageTraceSwitch.Sw.Info);
						throw;
					}
					finally
					{
						if (dr != null)
							dr.Close();
					}
                }
                else TbOrder.Value                   =   0;
            }
        }

		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("5E303177-7FD3-4a17-A9CD-AA09A1592D6B"); // HtmlTabs
				return al;
			}
		}
		/// <summary>
        /// create or update an HtmlTab.  It  uses the AddonNice.HtmlTabsDB()
        /// </summary>
        void HtmlTabsEdit_Update(object sender,AddonNice.UI.ADNPageEventArgs e)
        {
			try
			{
                int tabOrder        =   (TbOrder.Value.HasValue) ? (int)TbOrder.Value:0;
				if ( isNewItem ) 
				{
					// Add the HtmlTab within the HtmlTabs table
					ItemID  =   HtmlTabDB.AddTab(ModuleID,TitleField.Text,DesktopText.TextContent,cbIsBreak.Checked,tabOrder);
				}
				else 
				{
					// Update the HtmlTab within the HtmlTabs table
					HtmlTabDB.UpdateTabGlobalized(ModuleID, ItemID,TitleField.Text, DesktopText.TextContent,cbIsBreak.Checked,tabOrder);
				}
			}
			catch(Exception ex )
			{
				if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabsEdit OnUpdate ex : {0}",ex),PageTraceSwitch.Sw.Info);
				throw;
			}
        }

        /// <summary>
        /// The DeleteBtn_Click event handler on this Page is used to delete an
        /// an HtmlTab.  It  uses the AddonNice.HtmlTabsDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        void HtmlTabsEdit_Delete(object sender,AddonNice.UI.ADNPageEventArgs e)
        {
			// Only attempt to delete the item if it is an existing item
            if ( !isNewItem ) 
            {
				try
				{
					HtmlTabDB.DeleteTabGlobalized(ItemID);
				}
				catch(Exception ex )
				{
					if (PageTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabsEdit OnDelete ex : {0}",ex),PageTraceSwitch.Sw.Info);
                    throw;
				}
            }
        }

		#region Std overrides

        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        override protected void OnPreInit(EventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]HtmlTabsEdit OnPreInit Begin",PageTraceSwitch.Sw.Info);
            if ( isNewItem ) 
                PageHasDeleteBtn    =   false;
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HtmlTabsEdit OnPreInit PageHasDeleteBtn: {0}.",PageHasDeleteBtn),PageTraceSwitch.Sw.Info);
            base.OnPreInit(e);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]HtmlTabsEdit OnPreInit End",PageTraceSwitch.Sw.Info);
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() 
        {    
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]HtmlTabsEdit InitializeComponent Begin",PageTraceSwitch.Sw.Info);
			Load                +=  new System.EventHandler(this.Page_Load);
            Update              +=  new PageEventHandler(HtmlTabsEdit_Update);
            Delete              +=  new PageEventHandler(HtmlTabsEdit_Delete);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]HtmlTabsEdit InitializeComponent End",PageTraceSwitch.Sw.Info);
		}
        
		protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get	{ return PlaceHolderButtons; }
			set {}
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get	{ return PlaceHolderButtons2; }
			set {}
		}

		#endregion
    }
}

